package com.tczs.server;

import com.tczs.server.response.Response;
import io.netty.channel.ChannelDuplexHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.timeout.IdleState;
import io.netty.handler.timeout.IdleStateEvent;
import org.apache.log4j.Logger;

public class TimeOutHandler extends ChannelDuplexHandler {

    private static Logger logger = Logger.getLogger(TimeOutHandler.class);

    @Override
    public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
        if (evt instanceof IdleStateEvent) {
            IdleStateEvent e = (IdleStateEvent) evt;
            if (e.state() == IdleState.WRITER_IDLE) {
                logger.error("write time out");
                ctx.channel().writeAndFlush(Response.errorResponse());
                ctx.close();
            }
        }
    }
}
